using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Xml.Serialization;
using Jaws.Core.Web.Data;

namespace Jaws.Core.Web.Entities.Workspaces
{
    /// <summary>
    /// Contains methods and properties to persist and retrieve user personalization data.
    /// </summary>
    [Serializable]
    [XmlRoot("workspaces")]
    public class WorkspaceCollection : Collection<Workspace>
    {
        private readonly object _lockObject = new object();

        /// <summary>
        /// Initializes a new instance of the <see cref="WorkspaceCollection"/> class.
        /// </summary>
        public WorkspaceCollection()
        {
        }

        internal WorkspaceCollection(List<Workspace> workspaces)
        {
            foreach (Workspace workspace in workspaces)
            {
                Add(workspace);
            }

        }

        /// <summary>
        /// Gets or sets the shared personalization BLOB.
        /// </summary>
        /// <value>The shared personalization BLOB.</value>
        [XmlElement(typeof(byte[]), ElementName = "sharedPersonalization")]
        public byte[] SharedPersonalizationBlob { get; set; }

        /// <summary>
        /// Finds the by user and path.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public Workspace FindByUserAndPath(string userName, string path)
        {
            foreach (Workspace w in this)
            {
                if (string.Compare(w.UserName, userName, true, CultureInfo.CurrentCulture) == 0)
                {
                    if (string.Compare(w.Path, path, true, CultureInfo.CurrentCulture) == 0)
                    {
                        return w;
                    }
                }
            }

            return null;
        }

        /// <summary>
        /// Finds the by path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public Collection<Workspace> FindByPath(string path)
        {
            var spaces = new Collection<Workspace>();

            foreach (Workspace w in this)
            {
                if (string.Compare(w.Path, path, true, CultureInfo.CurrentCulture) == 0)
                {
                    spaces.Add(w);
                }
            }

            return spaces;
        }

        /// <summary>
        /// Finds the name of the by user.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <returns></returns>
        public Collection<Workspace> FindByUserName(string userName)
        {
            var spaces = new Collection<Workspace>();

            foreach (Workspace w in this)
            {
                if (string.Compare(w.UserName, userName, true, CultureInfo.CurrentCulture) == 0)
                {
                    spaces.Add(w);
                }
            }

            return spaces;
        }
    }
}